﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace PPI.General
{
    public class ConfigReader
    {
        public const string K_APPPATH = "C:\\PolePosition2.1\\Poleposition.config";
        public string ConectionMode
        {
            get { return m_ConectionMode; }
        }

        private string m_ConectionMode = "";
        private static volatile ConfigReader _instance = null;
        public static ConfigReader GetInstance()
        {
            if (_instance == null)
            {
                lock (typeof(ConfigReader))
                {
                    if (_instance == null)
                    {
                        _instance = new ConfigReader();
                    }
                }
            }
            return _instance;
        }

        protected ConfigReader()
        {
            //Check the existing file.
            if (!File.Exists(K_APPPATH))
            {
                ConfigFile();
            }
            m_ConectionMode = GetConnectionString();
        }

        private static void ConfigFile()
        {
            StreamWriter writer = File.CreateText(K_APPPATH);
            writer.WriteLine("APPLICATION:BETA");
            writer.Close();
        }

        public int GetConnectionMode()
        {
            StreamReader fr = new StreamReader(K_APPPATH);
            string line = fr.ReadLine();
            fr.Close();
            if (line.IndexOf(':') > -1)
            {
                string word = line.Split(':')[1];

                if (word == "PRODUCTIVE_PPI")
                    return 1;
                else
                {
                    ConfigFile();
                    return 0;
                }
            }
            else 
            {
                ConfigFile();
                return 0;
            }

          


        }

        public string GetConnectionString()
        {
            StreamReader fr = new StreamReader(K_APPPATH);
            string line = fr.ReadLine();
            fr.Close();
            if (line.IndexOf(':') > -1)
            {
                string word = line.Split(':')[1];

                if (word == "PRODUCTIVE_PPI")
                    return "DEV";
                else
                {
                    ConfigFile();
                    return "BETA";
                }
            }
            else
            {
                ConfigFile();
                return "BETA";
            }




        }

    }
}
